<template>
  <div class="bulletin">
    <el-form label-width="130px" @submit.native.prevent="Edit(id)">
      <el-form-item label="文化之声标题">
        <el-input v-model="info.title" placeholder="文化之声标题" />
      </el-form-item>
      <el-form-item label="文化之声内容">
        <tinymce :height="300" v-model="info.content" />
      </el-form-item>
      <el-form-item label="封面">
        <Single-image v-model="info.cover"/>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="info.type" @change="handleTypeChange">
          <el-option :value="1" label="视频" />
          <el-option :value="2" label="音频" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="info.type === 1" label="视频">
        <div style="max-width: 600px;">
          <Single-video v-if="!info.url" v-model="info.url"/>
          <video v-if="info.url" :src="info.url" style="width: 100%; height: auto;" controls/>
          <el-button type="primary" @click="clearVideo">清空视频</el-button>
        </div>
      </el-form-item>
      <el-form-item v-if="info.type === 2" label="音频">
        <Single-audio v-model="info.url"/>
        <el-button type="primary" @click="clearAudio">清空音频</el-button>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker v-model="info.created_at" type="datetime" placeholder="选择发布时间" value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import SingleImage from '@/components/Upload/SingleImage'
import SingleAudio from '@/components/Upload/SingleAudio'
import SingleVideo from '@/components/Upload/SingleVideo'
import Tinymce from '@/components/Tinymce'
import { parseTime } from '@/utils/index'
export default {
  components: {
    Tinymce,
    SingleImage,
    SingleAudio,
    SingleVideo
  },
  data() {
    return {
      id: this.$route.params.id,
      info: {},
      created_at: parseTime(new Date().getTime())
    }
  },
  mounted() {
    this.GetInfoById(this.id)
  },
  methods: {
    clearAudio() {
      this.info.url = '' // 清空已上传的音频 URL
    },
    clearVideo() {
      this.info.url = '' // 清空已上传的音频 URL
    },
    handleTypeChange() {
    },
    GetInfoById(id) {
      this.$http.get('culture_voice/' + id).then(response => {
        console.log(response)
        this.info = response.resource
      })
    },
    Edit(id) {
      const params = {
        title: this.info.title,
        content: this.info.content,
        cover: this.info.cover,
        url: this.info.url,
        created_at: this.info.created_at
      }
      this.$http.patch('culture_voice/' + id, params).then(response => {
        this.$success('操作成功')
        this.$router.push('/culture-voice')
      })
    }
  }
}
</script>

<style>
.bulletin .el-progress-circle {
  background: #fff;
}
.avatar-uploader {
  position: relative;
}
.avatar-uploader .percent {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  max-width: 500px;
  display: block;
}
</style>
